package cn.iocoder.yudao.module.book.controller.app.booksShell;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBookshelfRespVO;
//import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppCategoryRespVO;
//import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppClassifyRespVO;
import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppCollectionsRespVO;
import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBooksRespVO;
import cn.iocoder.yudao.module.book.dal.dataobject.bookshelf.BookshelfDO;
import cn.iocoder.yudao.module.book.service.books.BooksService;
import cn.iocoder.yudao.module.book.service.classify.ClassifyService;
import cn.iocoder.yudao.module.book.service.users.userDTOService;
import cn.iocoder.yudao.module.book.service.bookshelf.BookshelfService;
import cn.iocoder.yudao.module.book.service.collections.CollectionsService;
import cn.iocoder.yudao.module.book.service.records.RecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 中英文绘本")
@RestController
@RequestMapping("/api/user/book")
//测试完成
public class AppBooksController {

    @Resource
    private CollectionsService collectionsService;

    @Resource
    private BooksService bookService;

    @Resource
    private RecordsService recordsService;

    @Resource
    private BookshelfService bookshelfService;

    @Resource
    private userDTOService userService;

    @Resource
    private ClassifyService classifyService;

//    @GetMapping("/categories")
//    @Operation(summary = "获得类别表数据-一级分类")
//    public CommonResult<List<AppClassifyRespVO>> getCategory(@Valid @RequestParam("lang") Integer lang) {
//        List<AppClassifyRespVO> classifyList = classifyService.getClassifyList();
//        List<AppBookshelfRespVO> collection = bookshelfService.selectByUserId(userService.getCurrentUserId());
//        List<AppCollectionsRespVO> collections= new ArrayList<>();
//        for(AppBookshelfRespVO bookshelfRespVO:collection){
//            collections.add(collectionsService.selectByLang(bookshelfRespVO.getCollectionId(),lang));
//        }
//        return CommonResult.success(classifyList);
//    }



//    @GetMapping("/groups")
//    @Operation(summary = "获得中英文绘本列表-二级分类")
//    public CommonResult<List<AppBooksRespVO>> getBookGroups(@Valid @RequestParam("id") Integer id) {
//
////        List<AppBooksRespVO> bookshelfRespVOS = bookService.getByCollectionId(id);
////        for (AppBooksRespVO bookshelfRespVO:bookshelfRespVOS){
////            System.out.println(bookshelfRespVO);
////            bookshelfRespVO.setCreateTime(Arrays.toString(bookshelfRespVO.getCreateTime().toCharArray()));
////        }
//        return CommonResult.success(bookService.getByCollectionId(id));
//    }

    @GetMapping("/removeSet")
    @Operation(summary = "删除绘本集")
    public CommonResult<Boolean> removeSet(@Valid @RequestParam("id") Integer id) {
        System.out.println(userService.getCurrentUserId());
        return CommonResult.success(bookshelfService.deleteByBookId(userService.getCurrentUserId(),id));
    }

    @GetMapping("/removeGroup")
    @Operation(summary = "删除绘本组")
    public CommonResult<Boolean> removeGroup(@Valid @RequestParam("id") Integer id) {
        return CommonResult.success(bookshelfService.deleteByCollectionId(userService.getCurrentUserId(),id));
    }

    @GetMapping("/markGroupTop")
    @Operation(summary = "标记绘本组为置顶")
    public CommonResult<Boolean> markGroupTop(@Valid @RequestParam("id") Integer id) {
        return CommonResult.success(bookshelfService.markGroupTop(userService.getCurrentUserId(),id));
    }

    @GetMapping("/addToBookshelf")
    @Operation(summary = "添加集合到书架")
    public CommonResult<Boolean> addToBookshelf(@Valid @RequestParam("collectionId") Integer collectionId) {
        Long userId = userService.getCurrentUserId();
        List<AppBooksRespVO> books = bookService.getByCollectionId(collectionId);
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        for(AppBooksRespVO book : books) {
            bookshelfService.addBook(book.getBookId(), collectionId, userId.intValue(), time);
        }
        return CommonResult.success(true);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索绘本")
    public CommonResult<PageResult<AppCollectionsRespVO>> search(@Valid @RequestParam("content") String content,
                                                           @Valid @RequestParam("page") Integer page,
                                                           @Valid @RequestParam("pageSize") Integer pageSize) {
        List<AppCollectionsRespVO> books=collectionsService.selectLikeContent(content);

        Long total = (long) books.size();

// 防止 pageNum <= 0 的情况
        if (page <= 0) {
            page = 1; // 默认第一页
        }
//计算分页区间
        int start = (page - 1) * pageSize;
        int end = Math.min(start + pageSize, books.size());

// 防止 start 超出范围
        if (start > books.size()) {
            start = books.size(); // 如果页码超出范围，设置为最后一个位置
        }

// 截取当前页数据
        List<AppCollectionsRespVO> pagedList = books.subList(start, end);

        // 构造分页结果
        PageResult<AppCollectionsRespVO> pageBooks = new PageResult<>();
        pageBooks.setList(pagedList);
        pageBooks.setTotal(total);

        // 返回结果
        return success(BeanUtils.toBean(pageBooks, AppCollectionsRespVO.class));
    }

}
